package {{.ModuleName}}impl

import (
	"context"
	"github.com/stretchr/testify/require"
	"testing"

	"go-caipu/pkg/services/{{.PackageName}}/{{.ModuleName}}"
)

func Test{{.ClassName}}Service(t *testing.T) {
	{{.ModuleName}}Store := newFake{{.ClassName}}Store()
	{{.ModuleName}}Service := Service{store: {{.ModuleName}}Store}

    testModel:={{.ModuleName}}.{{.ClassName}}{
                       			    {{- range .Columns -}}
                                       {{- $z := .IsRequired -}}
                                       {{- if ($z) -}}
                                       {{- if eq .GoType "time.Time" -}} {{.GoField}}: time.Now(), {{- end -}}
                                       {{- if eq .GoType "int64" -}} {{.GoField}} : 1, {{- end -}}
                                       {{- if eq .GoType "string" -}} {{.GoField}} : "{{.GoField}}_InsertTest",{{- end -}}
                                       {{- if eq .GoType "int" -}} {{.GoField}} : 1,{{- end -}}
                                       {{- end -}}
                                       {{- end -}}
            }
	t.Run("{{.ModuleName}} Service:Insert", func(t *testing.T) {
			cmd := testModel

		err := {{.ModuleName}}Service.Insert(context.Background(), cmd)
		require.NoError(t, err)
	})

	t.Run("{{.ModuleName}} Service:Get{{.ClassName}}Page", func(t *testing.T) {
    		cmd := {{.ModuleName}}.Get{{.ClassName}}PageCommand{}
            {{.ModuleName}}Store.Expected{{.ClassName}}s = append({{.ModuleName}}Store.Expected{{.ClassName}}s, testModel)
		    {{.ModuleName}}Store.ExpectedCount = 1
    		result, count, err := {{.ModuleName}}Service.Get{{.ClassName}}Page(context.Background(), cmd)
    		require.NoError(t, err)
    		require.Equal(t, int64(1), count)
    		require.NotNil(t, 1, len(result))
    })

	t.Run("{{.ModuleName}} Service:Get", func(t *testing.T) {
		cmdGet := {{.ModuleName}}.Get{{.ClassName}}Command{}
		cmdGet.{{.PkGoField}} = 1
		{{.ModuleName}}Store.Expected{{.ClassName}} = testModel
		result, err := {{.ModuleName}}Service.Get(context.Background(), cmdGet)

		require.NoError(t, err)
		require.Equal(t, 1, result.{{.PkGoField}})
	})



	t.Run("{{.ModuleName}} Service:Update", func(t *testing.T) {
		cmd:=testModel
		err := {{.ModuleName}}Service.Update(context.Background(), cmd)
		require.NoError(t, err)
	})

	t.Run("{{.ModuleName}} Service:Remove", func(t *testing.T) {
		cmd := {{.ModuleName}}.Remove{{.ClassName}}Command{Ids: []int64{1}}
		err := {{.ModuleName}}Service.Remove(context.Background(), cmd)
		require.NoError(t, err)

	})
}

type Fake{{.ClassName}}Store struct {
	ExpectedError error
	ExpectedCount int64
	Expected{{.ClassName}}s []{{.ModuleName}}.{{.ClassName}}
	Expected{{.ClassName}}  {{.ModuleName}}.{{.ClassName}}
}

func (f Fake{{.ClassName}}Store) Get{{.ClassName}}Page(ctx context.Context, cmd {{.ModuleName}}.Get{{.ClassName}}PageCommand) (result []{{.ModuleName}}.{{.ClassName}}, num int64, err error) {
	return f.Expected{{.ClassName}}s,f.ExpectedCount,f.ExpectedError
}

func (f Fake{{.ClassName}}Store) Get(ctx context.Context, cmd {{.ModuleName}}.Get{{.ClassName}}Command) ({{.ModuleName}}.{{.ClassName}}, error) {
	return f.Expected{{.ClassName}}, f.ExpectedError
}

func (f Fake{{.ClassName}}Store) Insert(ctx context.Context, cmd {{.ModuleName}}.{{.ClassName}}) error {
	return f.ExpectedError
}

func (f Fake{{.ClassName}}Store) Update(ctx context.Context, cmd {{.ModuleName}}.{{.ClassName}}) error {
	return f.ExpectedError
}

func (f Fake{{.ClassName}}Store) Remove(ctx context.Context, cmd {{.ModuleName}}.Remove{{.ClassName}}Command) error {
	return f.ExpectedError
}

func newFake{{.ClassName}}Store() *Fake{{.ClassName}}Store {
	return &Fake{{.ClassName}}Store{}
}
